<template>
  <div class="WriteArticle">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>写文章</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top :15px;">
      <el-form
        :model="WarticleForm"
        status-icon
        :rules="WarticleRules"
        ref="WarticleRef"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="WarticleForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="选择分类" prop="category">
          <el-select v-model="WarticleForm.category" placeholder="请选择" @change="asd">
            <el-option
              v-for="item in cateList"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章摘要" prop="description">
          <el-input v-model="WarticleForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="文章封面">
          <el-upload
            class="avatar-uploader"
            action="/api/upload/upload"
            :show-file-list="false"
            :on-success="seccessAfter"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="WarticleForm.artcleImg" :src="WarticleForm.artcleImg" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="文章内容">
          <quill-editor v-model="WarticleForm.content"></quill-editor>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="saveArticle()">保存</el-button>
          <el-button @click="submitWriteForm()">发布</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import jwt_decode from 'jwt-decode'
export default {
  name: 'WriteArticle',
  data() {
    return {
      cateList: [],
      WarticleForm: {
        title: '',
        description: '',
        content: '',
        artcleImg: '',
        category: '',
        userId: '',
        isStatus: '',
        isCaogao: ''
      },
      userInfo: [],
      WarticleRules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
        ],

        category: [
          { required: true, message: '请输入文章分类', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入文章摘要', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getcategory()
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      const token = window.sessionStorage.getItem('token')
      var decoded = jwt_decode(token)
      console.log(decoded)
      this.userInfo = decoded
      this.WarticleForm.userId = decoded.id
    },
    getcategory() {
      this.$axios.get('/api/admin/category').then(result => {
        console.log(result.data)
        this.cateList = result.data
      })
    },
    asd() {
      console.log(this.WarticleForm)
    },

    seccessAfter(response, file, fileList) {
      const a = response.path.split('\\')
      this.WarticleForm.artcleImg = 'http://localhost:5000/' + a[a.length - 1]
      console.log(this.WarticleForm)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    //发布
    submitWriteForm() {
      this.$refs.WarticleRef.validate(valid => {
        if (valid) {
          this.WarticleForm.isCaogao = true
          console.log(this.WarticleForm)
          this.$axios
            .post('/api/admin/write', this.WarticleForm)
            .then(result => {
              this.$message({
                type: result.data.type,
                message: result.data.msg
              })
              this.$router.push({ name: 'MyArticle' })
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    saveArticle() {
      this.$refs.WarticleRef.validate(valid => {
        if (valid) {
          this.WarticleForm.isCaogao = false
          console.log(this.WarticleForm)
          this.$axios
            .post('/api/admin/save', this.WarticleForm)
            .then(result => {
              this.$message({
                type: result.data.type,
                message: result.data.msg
              })
              this.$router.push({ name: 'MyArticle' })
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  box-shadow: 0 0 10px #ccc;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>